//
//  TutorialsTableViewController.m
//  TutorialsModule
//
//  Created by Augusto Guido on 28/09/09.
//  Copyright 2009 Moove-IT. All rights reserved.
//

#import "TutorialsTableViewController.h"
#import "TutorialViewController2.h"
#import "NewTutorialViewController2.h"
#import "NewTutorialViewController2.h"

@implementation TutorialsTableViewController

@synthesize tutorials;

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

-(id) initWithNibName: (NSString*) nibNameOrNil bundle:(NSBundle *)nibBundleOrNil objects: (NSArray *)objects{

	if(self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
	{
		tutorials =  [[NSArray alloc] initWithArray:objects];
	}
	return self;
	
}


- (void)viewDidLoad {
    [super viewDidLoad];
	self.navigationItem.rightBarButtonItem = self.editButtonItem;
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//	self.tabBarController.tabBar.alpha = 0.8;
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
		return [tutorials count];
	else
		return 1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		if (indexPath.section == 0){
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];			
		}else{
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		}
    }
	
	if (indexPath.section == 0)
	{
		UIImageView *photo;		
        photo = [[[UIImageView alloc] initWithFrame:CGRectMake(240.0, 2.0, 50.0, 40.0)] autorelease];		
		photo.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon.jpg" ofType:nil]];		
        photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;		
        [cell.contentView addSubview:photo];
		
		UILabel *textLabel;
		textLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 5, 145, 20)] autorelease];
		textLabel.text = [tutorials objectAtIndex:indexPath.row];
		textLabel.font = [UIFont systemFontOfSize:14];
		[cell.contentView addSubview:textLabel];
		
		UILabel *detail;
		detail = [[[UILabel alloc] initWithFrame:CGRectMake(10, 30, 145, 10)] autorelease];
		detail.text = @"Video - Published 22/7/23";
		detail.font = [UIFont italicSystemFontOfSize:10];
		[cell.contentView addSubview:detail];
						
	}else{
		cell.textLabel.text = @"Add tutorial";
		cell.textLabel.textAlignment =UITextAlignmentCenter;
	}
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
    // Set up the cell...
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1){
		NewTutorialViewController2 *newTutorialViewController = [[NewTutorialViewController2 alloc] initWithNibName:@"NewTutorialViewController2" bundle:nil];
		[self.navigationController pushViewController:newTutorialViewController animated:YES];
		[newTutorialViewController release];
	}
	if (indexPath.section == 0){
		TutorialViewController2 *tutorialViewController = [[TutorialViewController2 alloc] initWithNibName:@"TutorialViewController2" bundle:nil];
		tutorialViewController.title = [tutorials objectAtIndex:indexPath.row];
		[self.navigationController pushViewController:tutorialViewController animated:YES];
		[tutorialViewController release];
	}
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    /*if (indexPath.section == 0)
		return YES;
	 else return NO;*/return YES;
}




// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}




// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
	
}


// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0)// Return NO if you do not want the item to be re-orderable.
		return YES;
	else return NO;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
	if (indexPath.section == 0)
		return UITableViewCellEditingStyleDelete;
	else return UITableViewCellEditingStyleInsert;
}


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
	if (indexPath.section == 0){
		TutorialViewController2 *tutorialViewController = [[TutorialViewController2 alloc] initWithNibName:@"TutorialViewController2" bundle:nil];
		tutorialViewController.title = [tutorials objectAtIndex:indexPath.row];
		[self.navigationController pushViewController:tutorialViewController animated:YES];
		[tutorialViewController release];
	}
};



- (void)dealloc {
	[tutorials release];
    [super dealloc];
}


@end

